//
//  SettingMenu.m
//  ios_news
//
//  Created by Han on 14-6-24.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "SettingMenu.h"

@implementation SettingMenu


- (id)initWithFrame:(CGRect)frame RootViewController:(UIViewController *)ctr
{
    if (self == [self initWithFrame:frame]) {
        self.mRootViewCtr = ctr;
        [self drawView];
        [self.mRootViewCtr.view addSubview:self];
    }
    return self;
}


- (void) drawView
{
    //self.layer.shadowOffset=CGSizeMake(0, -10);
    
    UIView *sliderView = [[UIView alloc]initWithFrame:CGRectMake(10, 30, 300, 40)];
    sliderView.backgroundColor = ColorWithRGB(219, 219, 219);
    [self addSubview:sliderView];
    
    UIImageView *leftImageView = [[UIImageView alloc] init];
    [leftImageView setImage:[UIImage imageNamed:@"sun1"]];
    leftImageView.frame = CGRectMake(10, (GET_VIEW_HEIGHT(sliderView)-15)/2, 15, 15);
    [sliderView addSubview:leftImageView];
    
    UIImageView *rightImageView = [[UIImageView alloc]init];
    rightImageView.frame = CGRectMake(GET_VIEW_WIDTH(sliderView)-10-20, (GET_VIEW_HEIGHT(sliderView)-20)/2, 20, 20);
    [rightImageView setImage:[UIImage imageNamed:@"sun2"]];
    [sliderView addSubview:rightImageView];
    
    UISlider *sliderA=[[UISlider alloc] init];
    sliderA.frame = CGRectMake(GET_LEFT_X(leftImageView)+10, (GET_VIEW_HEIGHT(sliderView)-34)*0.5, GET_VIEW_WIDTH(sliderView) - 4*10-35, 34);
    [sliderA addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    sliderA.backgroundColor = [UIColor clearColor];
    sliderA.minimumValue = 0;
    sliderA.maximumValue = 1.0;
    sliderA.value = [UIScreen mainScreen].brightness;

    
    [sliderView addSubview:sliderA];
    
    
    //设置字体
    UILabel *labelT = [[UILabel alloc]initWithFrame:CGRectMake(10, GET_BOTTOM_Y(sliderView)+10, 60, 40)];
    labelT.textColor = [UIColor darkGrayColor];
    labelT.text = @"字体大小";
    [labelT setFont:[UIFont boldSystemFontOfSize:15.0f]];
    
    UIView *selectFont = [[UIView alloc]initWithFrame:CGRectMake(320-10-220, GET_BOTTOM_Y(sliderView)+10, 220, 37)];
    // [selectFont setBackgroundColor:[UIColor redColor]];
    selectFont.layer.borderWidth = 0.5;
    selectFont.layer.masksToBounds = YES;
    [selectFont setBackgroundColor:[UIColor clearColor]];
    [self addSubview:selectFont];
    CGFloat x = selectFont.frame.size.width/4.0;
    NSArray *array = [NSArray arrayWithObjects:@"小",@"中",@"大",@"超大", nil];
    for (int i= 0 ; i<4; i++) {
        UIButton *fontBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        fontBtn.tag = i+1000;
        /*
        if ([Util isNightMode]) {
            [fontBtn setBackgroundColor:UIColorFromRGB(0x737373)];
        }
        */
        [fontBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        fontBtn.frame = CGRectMake(i*x, 0, x, 37);
        if (i!=3) {
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(fontBtn.frame.size.width-0.5, 0, 0.5, fontBtn.frame.size.height)];
            [line setBackgroundColor:[UIColor darkGrayColor]];
            [fontBtn addSubview:line];
        }
        [fontBtn setTitle:array[i] forState:UIControlStateNormal];
        [fontBtn setTitleShadowColor:[UIColor blackColor] forState:UIControlStateSelected];
        [fontBtn addTarget:self action:@selector(fontBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
        
        [selectFont addSubview:fontBtn];
    }
    
    [self addSubview:labelT];
    
    /*
    float width = (320-3*10)/2;
    //添加黑白选着
    UIButton *whiteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    whiteBtn.frame = CGRectMake(10, GET_BOTTOM_Y(selectFont)+10, width, 40);
    [whiteBtn setBackgroundColor:UIColorFromRGB(0xffffff)];
    whiteBtn.tag = 99;
    //[whiteBtn addTarget:self action:@selector(modeBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [whiteBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [whiteBtn setTitle:@"白天" forState:UIControlStateNormal];
    whiteBtn.backgroundColor = UIColorFromRGB(0xffffff);
    whiteBtn.layer.borderWidth = 0.5;
    whiteBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    [self addSubview:whiteBtn];
    
    
    UIButton *blckBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    blckBtn.tag = 999;
    //[blckBtn addTarget:self action:@selector(modeBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    blckBtn.frame = CGRectMake(GET_LEFT_X(whiteBtn)+10, GET_VIEW_ORIGIN_Y(whiteBtn), width, 40);
    [blckBtn setBackgroundColor:UIColorFromRGB(0x575757)];
    [blckBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [blckBtn setTitle:@"夜间" forState:UIControlStateNormal];
    [self addSubview:blckBtn];
    */
     
    // 阴影部分
    self.layer.shadowOffset=CGSizeMake(0, -5);
    self.layer.shadowColor = [UIColor darkGrayColor].CGColor;
    self.layer.shadowOpacity = 0.9;
    self.layer.shadowRadius = 3.0;
    self.backgroundColor = ColorWithRGB(246, 246, 246);
}


-(void)sliderValueChanged:(UISlider*)slider
{
    [[UIScreen mainScreen] setBrightness:slider.value];
}


//字体切换
- (void)fontBtnTapped:(id)sender
{
    NSString * sizeofFont = @"17";
    UIButton *button = (UIButton *)sender;
    NSString *title = [button titleForState:UIControlStateSelected];
    if ([title isEqualToString:@"小"])
    {
        sizeofFont = @"15";
    }
    else if([title isEqualToString:@"中"])
    {
        sizeofFont = @"17";
    }
    else if([title isEqualToString:@"大"])
    {
        sizeofFont = @"19";
    }
    else if([title isEqualToString:@"超大"])
    {
        sizeofFont = @"21";
    }

    if (self.mRootViewCtr && [self.mRootViewCtr respondsToSelector:@selector(fontChanged:)]) {
        [self.mRootViewCtr performSelector:@selector(fontChanged:) withObject:sizeofFont];
    }

}


- (void)show
{
    self.layer.shadowOffset = CGSizeMake(0, 5);
    [self.mRootViewCtr.view bringSubviewToFront:self];
    
    
    [UIView animateWithDuration:0.5f animations:^{
        self.frame = CGRectMake( 0, -20, 320, 150);
    }];
    
    
}

- (void)disMiss
{
    [UIView animateWithDuration:0.2f animations:^{
        self.frame = CGRectMake(0, 0, 320, 150);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.3 animations:^{
            self.frame = CGRectMake(0, -210, 320, 150);
            self.layer.shadowOffset = CGSizeMake(0, -5);
        }];
    }];

}

@end
